package CompletableFuture.常用方法;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @program: algorithm
 * @description: thenAccept
 * @author: 陈恩涛
 * @create: 2022-08-09 23:26
 **/
public class CfApiDemo3 {
    public static void main(String[] args) {
        final ExecutorService threadPool = Executors.newFixedThreadPool(5);
        final CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> {
//            try {
//                TimeUnit.MILLISECONDS.sleep(20);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            System.out.println("1号任务：" + Thread.currentThread().getName());
            return "abc";
        },threadPool).thenRun(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("2号任务：" + Thread.currentThread().getName());
        }).thenRun(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("3号任务：" + Thread.currentThread().getName());
        }).thenRun(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("4号任务：" + Thread.currentThread().getName());
        });
        System.out.println(completableFuture.join());
        threadPool.shutdown();
    }
}
